package com.ljb.lib_hiknvr.nvr.play;

import android.content.Context;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.hikvision.netsdk.HCNetSDK;
import com.ljb.lib_hiknvr.nvr.ThreadUtils;
import com.ljb.lib_hiknvr.nvr.controller.HikNVRController;


/**
 * Created by ljb on 2018/8/14.
 */
public class PlaySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
    private static final String TAG = "PlaySurfaceView";

    //Surface是否创建完成
    private boolean mCreated;
    //预览句柄
    private int mRealHandle = -1;
    //构建器
    private PlaySurfaceViewBuilder mBuilder;


    public PlaySurfaceView(PlaySurfaceViewBuilder builder) {
        super(builder.mContext);
        mBuilder = builder;
        getHolder().addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        ThreadUtils.runInThread(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG, "surfaceCreated... ");
                mCreated = true;
                startPlay();
            }
        });
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        ThreadUtils.runInThread(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG, "surfaceDestroyed... ");
                mCreated = false;
                stopPlay();
            }
        });
    }


    private OnPlayStateListener mOnPlayStateListener;

    public void setOnPlayStateListener(OnPlayStateListener onPlayStateListener) {
        mOnPlayStateListener = onPlayStateListener;
    }


    /**
     * PlaySurfaceView构建器
     */
    public static class PlaySurfaceViewBuilder {
        private Context mContext;
        //用户id
        private int mUserID = -1;
        //通道号
        private int mChannel = -1;
        //码流,默认是子码流
        private int mStreamType = 1;
        //录像保存路径
        private String mRecordPath;

        public PlaySurfaceViewBuilder(Context context) {
            mContext = context;
        }

        public PlaySurfaceView create() {
            return new PlaySurfaceView(this);
        }

        public PlaySurfaceViewBuilder setUserID(int userID) {
            this.mUserID = userID;
            return this;
        }

        public PlaySurfaceViewBuilder setChannel(int channel) {
            this.mChannel = channel;
            return this;
        }

        public PlaySurfaceViewBuilder setStreamType(int streamType) {
            this.mStreamType = streamType;
            return this;
        }

        public PlaySurfaceViewBuilder setRecordPath(String recordPath) {
            this.mRecordPath = mRecordPath;
            return this;
        }
    }


    //++++++++++++++++++++++++++++++分割线++++++++++++++++++++++++++++++++++++++


    /**
     * 开始预览
     */
    public boolean startPlay() {
        //等待SurfaceView创建成功
        while (!mCreated) {
            try {
                Thread.sleep(100);
                Log.e(TAG, "wait for surface create");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        mRealHandle = HikNVRController.getInstance().startPlay(mBuilder.mUserID, mBuilder.mChannel, mBuilder.mStreamType, this);
        Log.e(TAG, "startPlay: realHandle = " + mRealHandle);

        //开始预览失败
        if (mRealHandle == -1) {
            Log.e(TAG, "startPlay: 开始预览失败:" + HCNetSDK.getInstance().NET_DVR_GetLastError());
            if (mOnPlayStateListener != null)
                mOnPlayStateListener.onStartError();
            return false;
        }
        //开始预览成功
        else {
            if (mOnPlayStateListener != null)
                mOnPlayStateListener.onStartSuccess();
            Log.e(TAG, "startPlay: 开始预览成功");
            return true;
        }
    }

    /**
     * 停止预览
     *
     * @return
     */
    public boolean stopPlay() {
        if (mRealHandle < 0)
            return false;
        boolean ret = HikNVRController.getInstance().stopPlay(mRealHandle);
        //停止预览失败
        if (!ret) {
            Log.e(TAG, "stopPlay: 停止预览失败:" + HCNetSDK.getInstance().NET_DVR_GetLastError());
            if (mOnPlayStateListener != null)
                mOnPlayStateListener.onStopError();
            return false;
        }
        //停止预览失败
        else {
            mRealHandle = -1;
            if (mOnPlayStateListener != null)
                mOnPlayStateListener.onStopSuccess();
        }
        Log.e(TAG, "stopPlay: 停止预览结果成功");
        return true;
    }

    /**
     * 获取当前预览句柄
     *
     * @return
     */
    public int getRealHandle() {
        Log.e(TAG, "getRealHandle: " + mRealHandle);
        return mRealHandle;
    }

    /**
     * 设置构建器
     *
     * @param builder
     */
    public void setBuilder(PlaySurfaceViewBuilder builder) {
        mBuilder = builder;
    }
}
